<?php
namespace app\common\model;
use think\Model;
use think\Db;
/**
 * 
 * @author yq
 * 2017/08/25
 * 权限管理
 */
class Node extends Model{
    /**
     * 
     * @param string $nodeStr  节点ID,'1,2,3';
     * @param unknown $db_name 数据库名
     * @return array
     */
    public function getMenu($nodeStr = '',$db_name)
    {
        
       /*  dump($nodeStr);
        dump($db_name);
        exit; */
        //超级管理员没有节点数组
        $where = empty($nodeStr) ? 'status = 1 and menutitle=1' : 'status = 1  and menutitle=1 and id in('.$nodeStr.')';
        $result = Db::name($db_name)->where($where)->order('sort')->select();
        //$menu = prepareMenu($result);
        $menu = getTree($result);
        return $menu;
    }
    
    
    /**
     * 读取一条用户组信息
     * @param unknown $db_name 表名
     * @param unknown $where 条件
     * @param string $field 查询字段
     * @return array|\think\db\false|PDOStatement|string|\think\Model
     */
    public function getOneAuthGroup($db_name,$where,$field=''){
       return Db::name($db_name)->field($field)->where($where)->find();
    }
    
    
    
    /**
     * 修改用户组
     * @param unknown $where 条件
     * @param array $data  修改数据
     * @return Boolean true|false
     */
    public function modify($where,array $data,$db_name){
        
        $data['update_time'] = time(); //修改时间
        $id = Db::name($db_name)->where($where)->update($data);
        //写入log日志
        if(empty($id)){
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】修改用户组失败【id='.$where['id'].'】',0,$this->user_data['model'].'_log');
        }else{
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】修改用户组成功 【id='.$where['id'].'】',1,$this->user_data['model'].'_log');
        }
        return $id;
    }
    
    
    /**
     * 添加用户组
     * @param unknown $db_name 表名
     * @param array $data  添加数据
     * @return id|int  返回ID
     */
    public function AddAuthgroup($db_name,array $data){
        $data['add_time'] = time();//添加时间
        $id =  Db::name($db_name)->insertGetId($data);
        if(empty($id)){
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】添加用户组失败',0,$this->user_data['model'].'_log');
        }else{
            writelog($this->user_data['uid'],$this->user_data['username'],'用户【'.$this->user_data['username'].'】添加用户组成功',1,$this->user_data['model'].'_log');
        }
        return $id;
    }
    
    
    /**
     * 获取全部节点
     * @param unknown $where
     * @param unknown $db_name
     */
    public function getAllMenu($where,$db_name){
       return Db::name($db_name)->where($where)->select();
    }
    
    
    
    
    
}